import os
import easygui as g
dir=g.diropenbox('请选择一个文件夹：','浏览文件夹','c:\\Python学习')
typedict={}
countlines={}
target=['.c','.py','cpp','.cc','.java','pas','asm']
def finddir(path):
    list=os.listdir(path)
    for each in list:
        if os.path.isdir(each):
            typedict.setdefault('文件夹', 0)
            typedict['文件夹'] += 1
            finddir(each)
            os.chdir(os.pardir)

        else:
            pathsplit = os.path.splitext(each)[1]
            typedict.setdefault(pathsplit,0)
            typedict[pathsplit] += 1
def countline(path):
    list = os.walk(path)
    for each in list:
        for eachfile in each[2]:
            fullname=each[0]+os.sep+eachfile
            splitext=os.path.splitext(eachfile)[1]
            if splitext in target :
                with open(each[0]+os.sep+eachfile,'rU',encoding='utf-8') as f:
                    try:
                        countlines.setdefault(splitext,0)
                        countlines[splitext] += len(f.readlines())
                    except UnicodeDecodeError:
                        pass
            else:
                continue
finddir(dir)
countline(dir)
count=0
resu=[]
for eachline in countlines.keys():
    resu.append('【'+eachline+'】源文件共'+str(typedict[eachline])+'个，源代码'+str(countlines[eachline])+'行\n')
    count+=countlines[eachline]
result=''.join(resu)
g.textbox('您目前共累计编写了%d行代码，完成度%.2f%%\n离10万行代码还差%d行，请继续努力！'%(count,count/100000,100000-count),'统计结果',result)


